﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Windows.Forms;
using FrmMain;

namespace FMain
{
    public partial class FrmDgv : Form
    {
        public FrmDgv()
        {
            InitializeComponent();
        }
        Dao dao = new Dao();
        private void FrmDgv_Load(object sender, EventArgs e)
        {
            
                add();
         
            
        }

       

        private void add() {
            string sql = "select ID,Uname,fname,fnum,[time],Adress,Sprice,bol from [a] ,[User],food where [user].UId=a.Uid  and a.FId=food.Fid ";
            this.dataGridView1.DataSource = dao.GetTable(sql);

        }
        private void 签收ToolStripMenuItem_Click(object sender, EventArgs e)
        {
            if (this.dataGridView1.SelectedRows.Count > 0) {
                int id = Convert.ToInt32(this.dataGridView1.SelectedRows[0].Cells[0].Value);
                string sql = string.Format(@"update a set bol=1 where id =  " + id);
                if (dao.DoNonquery(sql) > 0)
                {
                    MessageBox.Show("修改成功");
                    add();
                }
                else {
                    MessageBox.Show("修改失败");
                }
            }
        }

        private void 删除ToolStripMenuItem_Click(object sender, EventArgs e)
        {
            if (this.dataGridView1.SelectedRows.Count > 0)
            {
              
           if (Convert.ToInt32(dataGridView1.SelectedRows[0].Cells[7].Value)==0) {
                    MessageBox.Show("未签收不可删除");
                    return;
                }

                int id = Convert.ToInt32(this.dataGridView1.SelectedRows[0].Cells[0].Value);
                string sql = string.Format(@"delete from  a  where id =  " + id);
                if (dao.DoNonquery(sql) > 0)
                {
                    MessageBox.Show("删除成功");
                    add();
                }
                else
                {
                    MessageBox.Show("删除失败");
                }
            }
        }
    }
}
